package com.glela.controller;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glela.cache.redis.RedisUtil;
import com.glela.common.constant.CFlagEnum;
import com.glela.constant.SmsConstant;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.request.RequestCheckCode;
import com.glela.platform.model.request.RequestUserModel;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseUser;
import com.glela.platform.service.CheckCodeService;
import com.glela.platform.service.SmsService;
import com.glela.platform.service.UserService;
import com.glela.platform.util.ParameterUtils;

@Controller("informationCheckController")
@RequestMapping(value = "/send")
public class InformationCheckController
{
	private static Logger logger = LoggerFactory.getLogger(InformationCheckController.class);

	@Resource
	private CheckCodeService checkCodeService;
	@Resource
	private UserService userService;
	
	@Resource
	RedisUtil redisUtil;
	
	@Resource
	private SmsService smsService;
	

	@RequestMapping(value = "/sendMsg", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> registerSendMsg(@RequestBody RequestCheckCode constan)
	{
		try {
			String mobile=constan.getPhone();
			int type = constan.getType();
			Integer sendMode = constan.getSendMode();
			sendMode = sendMode==null?SmsConstant.MODE_TEXT:sendMode;
			
			ErrorEnum errorEnum=ErrorEnum.C20000;

			ResponseUser user = userService.getUserByName(mobile);
			
			if (type == SmsConstant.TYPE_REGISTER) {//注册
				if (user != null) {
					errorEnum=ErrorEnum.C20108;
				}
			}else if(type == SmsConstant.TYPE_RESETPWD){//找回密码
				if (user == null) {
					errorEnum=ErrorEnum.C20111;
				}
			}else if(type == SmsConstant.TYPE_APPLY_CREDIT){//申请信用虚拟币
				sendMode = SmsConstant.MODE_VOICE;
				if (user == null) {
					errorEnum=ErrorEnum.C20111;
				}else{
					Integer status = smsService.getVoiceVerifyCodeStatus(CFlagEnum.apply_credit.getKey());
					if(!status.equals(1)){
						errorEnum=ErrorEnum.C29905;
					}
				}
			}else {
				errorEnum=ErrorEnum.C29906;
			}
						
			if(errorEnum.equals(ErrorEnum.C20000)){
				boolean res = smsService.sendVerifyCodeSms(mobile, type, sendMode);
				if(res){
					errorEnum=ErrorEnum.C10000;
				}else{
					errorEnum=ErrorEnum.C20112;
				}
			}
			return new ResponseEntity<>(new ResponseBaseModel(errorEnum), HttpStatus.OK);	
		}catch (Exception e) {
			logger.error("发送验证码异常:" , e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * 校检验证码
	 * @param userModel
	 * @return
	 */
	@RequestMapping(value="/checkCode",method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?>  checkCode(@RequestBody RequestUserModel userModel){
		try {
			String mobile=userModel.getMobilePhone(),code = userModel.getCode();
			int type = 1;
			if(ParameterUtils.validParams(mobile,code)){
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29903), HttpStatus.OK);
			}
			boolean re = smsService.checkVerifyCode(mobile, type, code);
			if(re) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
			} else {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20109), HttpStatus.OK);
			}
		} catch (Exception e) {
			logger.error("验证验证码异常:" ,e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	
}

